﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using Labthink.PCSoft.UniversalHost4.Model;
using Labthink.PCSoft.UniversalHost4.UserControls.MsgBox;
using Labthink.PCSoft.UniversalHost4.Utility;
using Report.Model;
using CheckBox = System.Windows.Controls.CheckBox;

namespace Report.Forms
{
    /// <summary>
    /// FrmRemove.xaml 的交互逻辑
    /// </summary>
    public partial class FrmRemove : Window
    {
        public List<FullData> Datas { get; set; }

        public List<SelectedData> Selecteds { get; set; }

        public List<int> Removes { get; set; }
        private const int Col = 3;

        public FrmRemove()
        {
            InitializeComponent();
            Resources.MergedDictionaries.Add(ReportCommon.GetLan);
        }

        private void BtnCancel_OnClick(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private void BtnReset_OnClick(object sender, RoutedEventArgs e)
        {
            Removes = null;
            DialogResult = true;
            Close();
        }

        private void BtnOk_OnClick(object sender, RoutedEventArgs e)
        {
            Removes = new List<int>();
            foreach (var data in Datas)
            {
                var cb = GridItems.FindName("cb" + data.DataNo) as CheckBox;
                if (cb?.IsChecked != null && cb.IsChecked.Value)
                {
                    Removes.Add(data.DataNo);
                }
            }

            if (Removes.Count == Datas.Count)
            {
                var message = LanguageManager.GetLanguageValue("CantRemoveAllParams");
                MyMessageBox.Show(this, message);
                return;
            }

            if (Removes.Count > 0)
            {
                DialogResult = true;
                Close();
            }
        }

        private void FrmRemove_OnLoaded(object sender, RoutedEventArgs e)
        {
            var rows = Math.Ceiling(Datas.Count / (double)Col).ToInt();
            for (int i = 0; i < rows; i++)
            {
                GridItems.RowDefinitions.Add(new RowDefinition{Height = new GridLength(40)});
            }

            var style = Resources["CheckBoxStyle"] as Style;
            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < Col; j++)
                {
                    var di = i * Col + j;
                    if (Datas.Count > di)
                    {

                        var cb = new CheckBox { Tag = Datas[di].DataNo, Margin = new Thickness(3, 0, 3, 0),Content =  Datas[di].Text };
                        if (Selecteds.All(s => s.Id != Datas[di].DataNo)) cb.IsChecked = true;
                        if (style != null) cb.Style = style;
                        GridItems.RegisterName("cb" + Datas[di].DataNo, cb);

                        Grid.SetRow(cb, i);
                        Grid.SetColumn(cb, j);
                        GridItems.Children.Add(cb);
                    }
                }
            }
        }
    }
}
